<?php
/**
 * @author xp
 */
$list = file_get_contents('./plain.txt');
$items = explode("\n", $list);

$handlers = [
    'vmess' => function ($uri) {
        if (strpos($uri, '?') !== false) {
            list($body, $args) = explode('?', $uri);
        } else {
            $body = $uri;
            $args = '';
        }
        $body = base64_encode($body);
        return 'vmess://' . $body . '?' . $args;
    },
    'ss' => function ($uri) {
        if (strpos($uri, '/') !== false) {
            list($body, $args) = explode('/', $uri);
        } else {
            $body = $uri;
            $args = '';
        }
        $body = base64_encode($body);
        return 'ss://' . $body . '/' . $args;
    },
];

$encoded = [];
foreach ($items as $rest) {
    $rest = trim($rest);
    if (empty($rest)) {
        $encoded[] = '';
        continue;
    }

    if ($rest[0] == '#') {
        $encoded[] = $rest;
        continue;
    }

    if (strpos($rest, '://') === false) continue;
    list($protocol, $rest) = explode('://', $rest);
    if (!array_key_exists($protocol, $handlers)) {
        echo 'unknown: ' . $protocol . "\n";
        continue;
    }
    $encoded[] = $handlers[$protocol]($rest);
}

foreach ($encoded as $line) {
    assert(is_string($line));
    echo $line . "\n";
}
